package dao;

import java.util.ArrayList;

import model.CityModel;
import model.CountryModel;
import model.WeatherModel;

/**
 * Klasse ist angelegt nur zum Testen der DAO-Klassen
 * 
 * @author Katharina Matei
 * @version 1.0.0
 * 
 */
public class TestDAO {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		ArrayList<CountryModel> countryListe = null;
		ArrayList<CityModel> cityListe = null;
		ArrayList<WeatherModel> weatherListe = null;
		CityDAO cityDAO = new CityDAO();
		CountryDAO countryDAO = new CountryDAO();
		WeatherDAO weatherDAO = new WeatherDAO();
		WeatherModel weather = new WeatherModel();
		CityModel city = new CityModel();
		CountryModel country = new CountryModel("");

		try {
			// Lese alle Countries mit name = "A%" aus der DB
			country.setName("A%");
			countryListe = countryDAO.getCountryByCountry(country);
			if (countryListe.size() > 0) {
				System.out.println("=========================================");
				System.out.println("Countries mit A*");
				System.out.println("=========================================");
				for (CountryModel m : countryListe) {
					country = m;
					System.out.println(m.getCountryID() + ", " + m.getName());
				}
			}

			// Lese alle Cities in Deutschland aus der DB
			cityListe = cityDAO.getCities("DE");
			if (cityListe.size() > 0) {
				System.out.println("=========================================");
				System.out.println("Cities by DE");
				System.out.println("=========================================");
				for (CityModel m : cityListe) {
					System.out.println(m.getCityID() + ", " + m.getName()
							+ ", " + m.getCountryID() + ", "
							+ m.getCountryName());
				}
			}
			// Lese alle Cities zum Country
			cityListe = cityDAO.getCityByCountry(country);
			if (cityListe.size() > 0) {
				System.out.println("=========================================");
				System.out.println("Cities by " + country.getName());
				System.out.println("=========================================");
				for (CityModel m : cityListe) {
					System.out.println(m.getCityID() + ", " + m.getName()
							+ ", " + m.getCountryID() + ", "
							+ m.getCountryName());
				}
			}
			// Ein Weather-Model in die DB schreiben
			long l = 1234567890123l;
			weather.setCityID("EDLW");
			weather.setInfo("Information 2");
			weather.setAirPressure(45);
			weather.setCloudiness("FEW040TCU");
			weather.setDate("2013/06/11 09:00");
			weather.setObservationTime("081620Z");
			weather.setTemperature(15);
			weather.setTimeStamp(l);
			weather.setVisibility(12);
			weather.setWindDirectionInfo("NORD");

			weatherDAO.insert(weather);

			// Lese alle Wetter-Daten in EDDS
			weatherListe = weatherDAO.getAllWeatherByCity("EDDS");
			if (weatherListe.size() > 0) {
				System.out.println("=========================================");
				System.out.println("Wetterdaten von EDDS");
				System.out.println("=========================================");

				for (WeatherModel m : weatherListe) {
					System.out.println(m.getCityID() + ", " + m.getDate()
							+ ", " + m.getInfo());
				}
			}

			// Lese aktuelle Wetterdaten in EDDS
			weather = weatherDAO.getAktWeatherByCity("EDDS");
			if (weather != null) {
				city = cityDAO.readCity(weather.getCityID());
				System.out.println("=========================================");
				System.out.println("Akt. Wetter in EDDS ");
				System.out.println("=========================================");
				System.out.println(weather.getCityID() + "; " + city.getName()
						+ "; Temperature: " + weather.getTemperature());
			}

			// Lese die Wetter-Daten in EDDS im Zeitinterval von/bis
			weatherListe = weatherDAO.getWeatherVonBis("EDDS",
					"2013-06-13 00:00:00", "2013-06-14 00:00:00");
			if (weatherListe.size() > 0) {
				System.out.println("=========================================");
				System.out
						.println("EDDS von 2013-06-13 00:00:00 bis 2013-06-14 00:00:00");
				System.out
						.println("============================================");
				for (WeatherModel m : weatherListe) {
					System.out.println(m.getCityID() + ", " + m.getDate()
							+ ", " + m.getInfo());
				}
			}
			System.out.println("=========================================");
			CityModelDBTest();
			System.out.println("=========================================");
			CountryModelDBTest();

			// Loesche Wetter-Daten aelter als der uebergebene Timestamp
			weatherDAO.delete("2013-06-12 12:00:00");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Test DB-Zugriffe fuer Country-Model
	 * 
	 * @throws DBException
	 */
	private static void CountryModelDBTest() throws DBException {

		CountryModel country = new CountryModel("");
		CountryDAO countryDAO = new CountryDAO();

		// Ein Country-Model in die DB schreiben
		country.setCountryID("XX");
		country.setName("TestCountry");
		countryDAO.insert(country);

		country = countryDAO.readCountry("XX");
		System.out.println("Insert-Country-Test: " + country.getCountryID()
				+ ", " + country.getName());

		// Country-Model mit country_id = "TT" in der DB aendern
		country.setCountryID("XX");
		country.setName("TestCountry22");
		countryDAO.update(country);

		country = countryDAO.readCountry("XX");
		System.out.println("Update-Country-Test: " + country.getCountryID()
				+ ", " + country.getName());

		// Country-Model mit country_id = "TT" aus der DB loeschen
		country.setCountryID("XX");
		countryDAO.delete(country);

		country = countryDAO.readCountry("XX");

		if (country == null) {
			System.out.println("Country-XX gelöscht");
		} else {
			System.out.println("Delete-Country-Test: " + country.getCountryID()
					+ ", " + country.getName());
		}
	}

	/**
	 * Test DB-Zugriffe fuer City-Model
	 * 
	 * @throws DBException
	 */
	private static void CityModelDBTest() throws DBException {

		CityModel city = new CityModel();
		CityDAO cityDAO = new CityDAO();

		// Ein City-Model in die DB schreiben
		city.setCityID("PAVL");
		city.setCountryID("KZ");
		city.setCountryName("Kazakhstan");
		city.setName("Pavlodar");
		cityDAO.insert(city);

		city = cityDAO.readCity("PAVL");
		System.out.println("Insert-City-Test: " + city.getCityID() + ", "
				+ city.getName() + ", " + city.getCountryID() + ", "
				+ city.getCountryName());

		// City-Model mit city_id = "PAVL" in der DB aendern
		city.setCityID("PAVL");
		city.setName("Pavlodar12");
		cityDAO.update(city);

		city = cityDAO.readCity("PAVL");
		System.out.println("Update-City-Test: " + city.getCityID() + ", "
				+ city.getName() + ", " + city.getCountryID() + ", "
				+ city.getCountryName());

		// City-Model mit city_id = "PAVL" aus der DB loeschen
		city.setCityID("PAVL");
		cityDAO.delete(city);

		city = cityDAO.readCity("PAVL");
		if (city == null) {
			System.out.println("City-PAVL gelöscht");

		} else {
			System.out.println("Delete-City-Test: " + city.getCityID() + ", "
					+ city.getName() + ", " + city.getCountryID() + ", "
					+ city.getCountryName());
		}
	}
}
